<template>
    <div class="qrcode-wrapper">
        <centerHeader/>

        <div class="qrcode-wrap">
            <div class="top-wrap">
                <div class="title_box">
                    <span class="title-01">我的店员</span>
                    <i class="el-icon-arrow-down"></i>
                </div>
            </div>
            <div class="picker">
                <div class="submit-box">
                    <button class="submit-btn" @click="add_clerk">添加店员</button>
                </div>
                <div>
                    <div style="margin-top: 15px;" class="select_query">
                        <el-input placeholder="请输入内容" v-model="search" class="input-with-select">
                            <el-button slot="append" icon="el-icon-search" @click="query_shop1"></el-button>
                        </el-input>
                    </div>

                    <div class="shop_box">
                        <transition name="slide-fade" class="tran_box">
                            <div class="hotKey-wrap" v-if="isDetail">
                                <div class="shop_detail">
                                    <i class="el-icon-close shop_close" @click="close"></i>
                                    <div class="add_clerk">
                                        <div class="title">{{this.store_user_status}}店员</div>
                                        <el-form  ref="formLabel" :label-position="labelPosition" label-width="2rem" :model="formLabel">
                                            <el-form-item label="姓名">
                                                <el-input  type="text" v-model="formLabel.username"  clearable></el-input>
                                            </el-form-item>
                                            <el-form-item label="手机号">
                                                <el-input  type="text" v-model="formLabel.mobile"  clearable></el-input>
                                            </el-form-item>
                                            <el-form-item label="佣金比例(%)">
                                                <el-input  type="text" v-model="formLabel.commission_bili"  clearable></el-input>
                                            </el-form-item>
                                            <el-form-item label="密码">
                                                <el-input  type="password" v-model="formLabel.password"  clearable></el-input>
                                            </el-form-item>
                                            <el-input  type="hidden" v-model="formLabel.uid"  clearable></el-input>
                                        </el-form>
                                        <div class="submit-box">
                                            <button class="submit-btn"  @click="submit">提交信息</button>
                                        </div>
                                    </div>

                                </div>
                            </div>
                        </transition>
                        <transition name="slide-fade" class="tran_box">
                            <div class="hotKey-wrap" v-if="isCustomer">
                                <div class="shop_detail">
                                    <i class="el-icon-close shop_close" @click="close1"></i>
                                    <div class="add_clerk1">
                                        <div>{{clerkItem.username}}</div>
                                        <div><i class="el-icon-phone"></i> {{clerkItem.mobile}}</div>
                                    </div>
                                    <div class="custom_box">
                                        <div class="custom_title">我的顾客</div>
                                        <ul class="custom_ul clearfix" v-for="item in customerlist" :key="item.uid">

                                            <router-link
                                                :to="{ name: '/center/customerDetail', query: {id:item.uid} }"
                                            >
                                                <li><a>{{item.username}}</a></li>
                                            </router-link>
                                        </ul>
                                    </div>

                                </div>
                            </div>
                        </transition>
                        <div class="shop_table_header">
                            <div class="th_one">姓名</div>
                            <div class="th_two">电话</div>
                            <div class="th_three">编辑</div>
                        </div>
                        <ul class="shop_table_table" v-for="item in shoplist" :key="item.uid">
                            <li >
                                <div class="td_one">
                                    <div class="td_item_one">{{item.username}}</div>

                                </div>
                                <div class="td_two">
                                    <div class="td_item_one">{{item.mobile}}</div>
                                </div>
                                <div class="td_three">
                                    <i class="el-icon-edit-outline" @click="edit_clerk(item)"></i>
                                    <i class="el-icon-delete" @click="delete_clerk(item)"></i>
                                    <!-- <i class="el-icon-tickets" @click="get_detail(item)"></i> -->
                                </div>
                            </li>
                        </ul>
                    </div>

                </div>
            </div>

        </div>
        <div class="submit-box" v-if="shoplist.length > 5">
            <button class="submit-btn" @click="query_shop">下拉加载更多</button>
        </div>
        <centerFooter/>
    </div>
</template>

<script type="text/ecmascript-6">
    import centerHeader from "@/views/center/components/centerHeader";
    import centerFooter from "@/views/center/components/centerFooter";
    import constant from "@/utils/constant.js";
    import searchPrdt from "../components/search";
    import { formatTime } from "@/utils/index.js";
    // 引用API文件
    import http from "@/utils/http.js";

    import QRCode from "qrcode";

    export default {
        name: "saleman",
        data() {
            return {
                user_info: {},
                startDate:"",
                endDate: '',
                isDetail:false,
                isCustomer:false,
                searchtype: '',
                search: '',
                shoplist:[],
                shopDetail:{},
                item:{},
                p:0,
                labelPosition: 'left',
                formLabel: {
                    username: '',
                    mobile: '',
                    commission_bili:"",
                    password: '',
                    uid:0
                },
                clerkItem:{
                    username:"",
                    mobile:"",

                },
                customerlist:[],
                store_user_status: '添加',
                is_add: true
            };
        },
        components: {
            centerHeader,
            centerFooter,
            searchPrdt,
        },
        methods: {

            clickSearch(val) {
                this.isSearch = val;
            },
            query_shop(){
                this.p++;
                http.get(this, {
                    url: "store/user_list",
                    data: {
                        token: localStorage.getItem("user_token"),
                        search:this.search,
                        pagesize:5,
                        p:this.p
                    },
                    dataType: "json",
                    success: function(data) {
                        let info = data.info
                        if(data.status == 1){
                            if (this.p <= info.total_page) {
                                this.shoplist = this.shoplist.concat(info.list);
                            }
                        }else{
                            this.$message.error(data.info);
                        }

                    }
                })
            },
            showDetail(id){
                http.get(this, {
                    url: "/store/detail",
                    data: {
                        token: localStorage.getItem("user_token"),
                        store_id:id
                    },
                    dataType: "json",
                    success: function(data) {
                        console.log(data)
                        if(data.status == 1){
                            this.item = data.info
                            this.isDetail = true
                        }else{
                            this.$message.error(data.info);
                        }

                    }
                })
            },
            close(){
                this.isDetail = false
            },
            close1(){
                this.isCustomer = false
            },
            submit(){
                if(this.is_add){
                    if(this.formLabel.username == "" || this.formLabel.mobile == "" || this.formLabel.commission_bili == "" || this.formLabel.password ==""){
                        this.$message.error('请您填写店员信息')
                        this.is_add = true;
                        return false
                    }

                    this.is_add = false;
                    if(!this.formLabel.uid){
                        http.post(this, {
                            url: "store/user_add",
                            data: {
                                token: localStorage.getItem("user_token"),
                                username: this.formLabel.username,
                                mobile: this.formLabel.mobile,
                                commission_bili: this.formLabel.commission_bili,
                                password: this.formLabel.password,
                                uid: this.formLabel.uid
                            },
                            dataType: "json",
                            success: function(data) {
                                console.log(data)
                                if(data.status == 1){
                                    this.$message.success('添加店员成功');
                                    this.isDetail = false
                                    this.formLabel = {}
                                    this.query_shop1()
                                    this.is_add = true;
                                }else{
                                    this.$message.error(data.info);
                                    this.is_add = true;
                                }
                            },
                            error: function(data){
                                this.$message.error('提交失败');
                            }
                        })
                    }else{
                        http.post(this, {
                            url: "store/user_edit",
                            data: {
                                token: localStorage.getItem("user_token"),
                                username: this.formLabel.username,
                                mobile: this.formLabel.mobile,
                                commission_bili: this.formLabel.commission_bili,
                                password: this.formLabel.password,
                                uid: this.formLabel.uid
                            },
                            dataType: "json",
                            success: function(data) {
                                console.log(data)
                                if(data.status == 1){
                                    this.$message.success('修改店员成功');
                                    this.isDetail = false
                                    this.formLabel = {}
                                    this.query_shop1()
                                    this.is_add = true;
                                }else{
                                    this.$message.error(data.info);
                                    this.is_add = true;
                                }
                            },
                            error: function(data){
                                this.$message.error('提交失败');
                            }
                        })
                    }
                }
            },
            edit_clerk(item){
                this.formLabel = item
                this.formLabel.password = ''
                this.isDetail = true;
                this.store_user_status = '编辑'
            },
            add_clerk(){
                this.formLabel = {
                    username: '',
                    mobile: '',
                    commission_bili:"",
                    password: '',
                    uid:0
                }
                this.isDetail = true;
                this.store_user_status = '添加'
            },
            delete_clerk(item){
                http.post(this, {
                    url: "/store/user_delete",
                    data: {
                        token: localStorage.getItem("user_token"),
                        store_id:item.store_id,
                        uid:item.uid
                    },
                    dataType: "json",
                    success: function(data) {
                        console.log(data)
                        if(data.status == 1){
                            this.query_shop1()
                        }else{
                            this.$message.error(data.info);
                        }

                    }
                })
            },
            get_detail(item){


                http.get(this, {
                    url: "/store/user_detail",
                    data: {
                        token: localStorage.getItem("user_token"),
                        store_id:item.store_id,
                        uid:item.uid
                    },
                    dataType: "json",
                    success: function(data) {
                        console.log(data)
                        if(data.status == 1){
                            this.clerkItem = data.info
                            //this.get_customer(item.store_id)
                        }else{
                            this.$message.error(data.info);
                        }

                    }
                })
                /*this.$router.push({
                    path:"/center/customerDetail",
                    query:{
                        id:item.uid
                    }
                })*/
            },
            get_customer(id){
                http.get(this, {
                    url: "/ucenter/user_info",
                    data: {
                        token: localStorage.getItem("user_token"),
                        uid:id
                    },
                    dataType: "json",
                    success: function(data) {
                        console.log(data)
                        if(data.status == 1){
                            this.customerlist = data.info
                            this.isCustomer = true
                        }else{
                            this.$message.error(data.info);
                        }

                    }
                })
            },
            query_shop1(){

                this.p = 0;
                this.shoplist = []
                this.query_shop()
            },
        },
        created() {

        },
        activated(){
            this.query_shop1()
        },
        watch: {
            $route() {
                console.log("监听");
                this.shoplist = [];
                this.p = 0;
                this.query_shop();
            }
        },
    };
</script>

<style scoped lang="scss">
    .qrcode-wrapper {
    }

    .top-wrap {
        $letHeight: 1.3rem;

        .title-txt {
            height: $letHeight;
            line-height: $letHeight;
            text-align: center;
            background-color: $c-white-light;

            span {
                color: $c-mainC;
                display: inline-block;
                line-height: $letHeight;
                vertical-align: middle;

                &.title-01 {
                    font-size: 0.38rem;
                }
            }
        }
    }

    .main-wrap {
        @extend .cmm-border;
    }

    .img-wrap {
        width: 5rem;
        height: 5rem;
        margin: 0.4rem auto;
        overflow: hidden;
        position: relative;
        padding: 0.22rem;

        .ad-img {
            @extend .cmm-translateXY;
            width: 100%;
        }
    }

    .qrcode-box {
        text-align: center;
        width: 100%;
        .qrcode-txt {
            width: 5.15rem;
            border-radius: 0.8rem;
            background-color: $c-danger;
            font-size: 0.52rem;
            color: $c-white-light;
            $letHeight: 0.6rem;
            height: $letHeight;
            line-height: $letHeight;
            text-align: center;
            margin: 0.48rem auto;
            display: inline-block;
            letter-spacing: 0.1rem;
            font-weight: bold;
        }
    }
    .title-txt-new{
        height:1rem;
        line-height:1rem;
        text-align: center;
        background-color: $c-white-light;
        font-size: 0.32rem;
    }
    .picker{
        padding:0 10px;
        border-top: 0.15rem solid #eeeeee;
        font-size: 0.3rem;
        .el-date-editor.el-input, .el-date-editor.el-input__inner{
            width:2.2rem;
            font-size: 0.3rem;
        }

    }
    .timer_box{
        /deep/.el-input__inner{
            border: none;
        }
    }
    .shop_table_header{
        display: flex;
        line-height:1rem;
        padding:0 0.2rem;
        border-bottom:1px solid #eeeeee;
        &>div.th_one{
            flex:1
        }
        &>div.th_two{
            flex:1
        }
        &>div.th_three{
            flex:1
        }
    }
    .shop_table_table{
        &>li{
            padding:0.25rem 0.2rem;
            display: flex;
            border-bottom:0.0133rem solid #eeeeee;
            &>div.td_one{
                flex:1;
                &>div.td_item_one{
                    margin-bottom:0.2rem;
                }
                &>div.td_item_two{
                    &>span{
                        font-size: 0.08rem;
                    }

                }
            }
            &>div.td_two{
                flex:1;
                &>div.td_item_one{
                    margin-bottom:0.2rem;
                }
                &>div.td_item_two{
                    &>span{
                        font-size: 0.08rem;
                    }

                }
            }
            &>div.td_three{
                flex:1;
                &>i{
                margin:0 0.1rem;
                font-size: 0.26rem;
            }
            }
        }
    }
    .title_box{
        height: 0.9rem;
        line-height:0.9rem;
        font-size: 0.36rem;
        text-align: center;
    }
    .timer_box{
        height:0.8rem;
        line-height:0.8rem;
    }
    .hot-key {
        width: 100%;
        min-height: 6rem;
        span {
            display: inline-block;
            width: 30%;
            height: 0.5rem;
            line-height: 0.5rem;

            text-align: center;
            font-size: 0.32rem;
            margin: 0.1rem 0;
            margin-right: 0.12rem;
            background-color: $c-gray-dark;
            color: $c-mainC;
            border-radius: 0.06rem;
        }
    }
    .shop_detail{
        position: relative;
        .shop_name{
            height:1rem;
            line-height:1rem;
            padding:0 0.4rem;

            font-size: 0.3rem;
        }
        .shop_other_box{
            border-bottom:0.013333rem solid #eeeeee;
            border-top:0.013333rem solid #eeeeee;
        }
        .shop_other{
            height: 0.8rem;
            line-height:0.8rem;
            padding:0 0.4rem;
        }
        .shop_price{
            height:4rem;
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 0.32rem;
            color:#c09a5b;
        }
    }
    .select_query{
        /deep/.el-input-group__prepend{
            width:1.6rem;
        }
        /deep/.el-input-group__append{
            width:1rem;
            text-align: center;
        }
        /deep/.input-with-select{
            height:0.8rem;
        }
        /deep/.el-input__inner{
            height:0.8rem;
            max-width: 7rem;
            font-size: 0.3rem;
        }
    }
    .shop_box{
        min-height:6rem;
        position: relative;
    }
    .slide-fade-enter-active {
        transition: all .3s ease;

    }
    .slide-fade-leave-active {
        transition: all .8s cubic-bezier(1.0, 0.5, 0.8, 1.0);
    }
    .slide-fade-enter, .slide-fade-leave-to
        /* .slide-fade-leave-active for below version 2.1.8 */ {
        transform: translateX(-100%);
        opacity: 0;
    }
    .tran_box{
        transform: translateX(-100%);
    }
    .shop_close{
        position: absolute;
        right: 0.3rem;
        top:0.32rem;
        font-size: 0.32rem;
    }
    .add_clerk{
        padding-top:0.8rem;
        .title{
            font-size: 0.3rem;
            margin-bottom: 0.6rem;
            margin-left:0.2rem;
        }
        /deep/.el-input__inner{
            border-top:none;
            border-left:none;
            border-right:none;

        }
        /deep/.el-form-item__label{
            font-size: 0.3rem!important;
        }
    }
    /deep/.el-form-item{
        margin-bottom:0.4rem;
    }
    /deep/.el-form-item__label{
        text-align: center;
    }
    .add_clerk1{
        padding:0.8rem 0.25rem 0 0.25rem;
        font-size: 0.24rem;
        border-bottom:0.01333rem solid #eeeeee;
        &>div{
            margin-bottom:0.25rem;
        }
    }
    .custom_box{
        .custom_title{
            height:1rem;
            line-height: 1rem;
            text-align: center;
            font-size: 0.3rem;
        }
        .custom_ul{
            li{
                width:25%;
                float:left;
                margin-bottom:0.2rem;
                &>a{
                    margin:0 0.1rem;
                    background-color: #eeeeee;
                    display:block;
                    line-height: 0.6rem;
                    text-align: center;
                }
            }
        }
    }
    /deep/.el-input__inner{
        font-size: 0.3rem;
    }
</style>
